package com.yazi.mxz.admin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.yazi.mxz.common.controller.BaseController;
import com.yazi.mxz.common.entity.beans.Goods;
import com.yazi.mxz.common.service.CategoryService;
import com.yazi.mxz.common.service.GoodsService;
import com.myframework.smhj.common.tool.PageLink;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Sort;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller("goodsController")
@RequestMapping("/goods")
public class GoodsController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(GoodsController.class);

	@Resource
	GoodsService goodsService;
	@Resource
	CategoryService categoryService;

	
	@RequestMapping("/list")
	public String list(Goods goods, Searching si, Model model) {
		log.debug("管理->进入商品列表页面");
		List<Sort> sorts = new ArrayList<Sort>();
		sorts.add(new Sort("goodsId", true));
		si.setSorts(sorts);
		Page<Goods> page = goodsService.list(goods, si);
		PageLink pageLink = new MyPageLink(page.getPaging());
		model.addAttribute("goodsList", page.getList());
		model.addAttribute("pageLink", pageLink);
		model.addAttribute("goods", goods);
		model.addAttribute("searchWord", si.getSearchWord());
		return "goods_list";
	}

	@RequestMapping("/addOrEdit")
	public String addOrEdit(Goods goods, Model model) {
		Long goodsId = goods.getGoodsId();
		if (null != goodsId) {
			log.debug("管理->进入商品编辑页面：goodsId={}", goodsId);
			goods = goodsService.one(goodsId);
		} else {
			log.debug("管理->进入商品添加页面");
		}
		model.addAttribute("goods", goods);
		return "goods_addOrEdit";
	}

	@RequestMapping("/addOrEditDo")
	public String addOrEditDo(Goods goods) {
		log.debug("管理->保存（添加/更新）商品");
//		goodsService.addOrUpdate(goods);
		return "redirect:/goods/list";
	}
	
}
